package cn.com.homeyoda.aimate.aihealth.ui.game.record;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.MessageTitleBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityGameRecordBinding;
import cn.com.homeyoda.aimate.aihealth.ui.game.analysis.GameAnalysisActivity;
import cn.com.homeyoda.aimate.aihealth.ui.message.MessageTitleAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.ui.message.MessageTitleAdapter;

public class GameRecordActivity extends BaseActivity<ActivityGameRecordBinding, GameRecordViewModel> implements View.OnClickListener {

    private GameRecordAdapter gameRecordAdapter;

    private cn.com.homeyoda.aimate.aihealth.ui.message.MessageTitleAdapter messageTitleAdapter;
    private List<MessageTitleBean> titleBeanList = new ArrayList<>();

    @Override
    public GameRecordViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(GameRecordViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_game_record;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        binding.ivBack.setOnClickListener(this);

        viewModel.titleData();
        viewModel.getGameRecordData();

        initRecyclerView();
    }

    private void initRecyclerView() {

        messageTitleAdapter = new MessageTitleAdapter(R.layout.message_title_item, titleBeanList, getApplicationContext());
        binding.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        binding.setAdapter(messageTitleAdapter);

        messageTitleAdapter.setOnItemClickListener((adapter, view, position) -> {
            messageTitleAdapter.setPos(position);
            viewModel.getGameRecordDataByType(position);
        });

        gameRecordAdapter = new GameRecordAdapter(R.layout.game_record_item);
        binding.setLayoutManager1(new LinearLayoutManager(this));
        binding.setAdapter1(gameRecordAdapter);

        gameRecordAdapter.setOnItemChildClickListener((adapter, view, position) ->
                startActivity(new Intent(this, GameAnalysisActivity.class)));
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.titleData.observe(this, messageTitleBeans -> {
            titleBeanList = messageTitleBeans;
            messageTitleAdapter.setNewData(titleBeanList);
        });

        viewModel.gameRecordData.observe(this, list -> gameRecordAdapter.setNewData(list));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

}